package com.hodi.myfirstapp
interface Study {
    fun readBooks()
}

var str:String? = null
fun  main (args: Array<String>) {
    val a = 1
    println(a)

    var c = 2
    var b = if (c == 2) {
        "c is 2"
    } else {
        "c is not 2"
    }
    println(b)

    when {
        c == 2 -> println("c is 2")
        else -> println("c is not 2")
    }

    when (c) {
        1 -> println("c is 1")
        2 -> println("c is 2")
        else -> println("c is not 1 or 2")
    }


    // 匿名函数

    val getStringLen =  { input: String ->  input.length }

    println(getStringLen("ddd"))



    class Car(val wheels: List<String>) {

       public var doorLook:Int = 241

        fun getDoorLook2(): Int {
            return doorLook
        }
    }

    val car = Car(listOf("left", "right"))
    car.doorLook = 444
    println(car.doorLook)


    val list = mutableListOf<String>("a", "b")
    list.add("c2")
    for (i in list) {
        println(i)
    }
    println(list)

    println(list.maxBy { it.length })


    val map = mapOf<String, Int>("a" to 1, "b" to 2)
    for ((k, v) in map) {
        println("$k : $v")
    }
    println(map["a"])

    open class Person(var age:String) {
        init {
            this.age = age
        }
        open var name = "xiaoming"
            get() {
                return field
            }
            set(value) {
                field = value + " student"
            }

        fun say () {
            println("hello laotie!")
        }
    }
    var personOne = Person("26")
    personOne.say() // 输出hello laotie!
    println(personOne.name) // xiaoming
    personOne.name = "daming"
    println(personOne.name) // daming student




    // 当继承类有主构造函数时，子类可以覆盖也可以继承时写死
    // 第一种写法写死16岁class Student(val code:String) : Person("16") {
    // 第二种写法覆盖age. class Student(val code:String, age:Strimg) : Person(age) {
    class Student(val code:String, age:String) : Person(age), Study {
        override var name = "xiaoye"
         init {
//             this.name = "xiaoye"
         }

        override fun readBooks() {
            println("i read the book 红楼梦")
        }

    }
    var studentOne = Student("12", "16")
    println(studentOne.name) // xiaoye
    println(studentOne.age) // 16
    studentOne.readBooks()


//    数据类和单例

    data class MeterData(var meterFid:String)

    val meterData = MeterData("10244252555")
    println(meterData.meterFid)
    meterData.meterFid = "45645464655"
    println(meterData.meterFid) // 45645464655


    val fruitList = listOf("苹果", "香蕉")
    for (item in fruitList) {
        println(item)
    }

    val phoneBrandList = mutableListOf<String>("华为", "小米")
    phoneBrandList.add("三星")
    for (item in phoneBrandList) {
        println(item)
    }

    val fruitSet = setOf<String>("小米", "苹果", "小米")
    for (item in fruitSet) {
        println(item)
    }

    val fruitMutableSet = mutableSetOf<String>("小米", "苹果")
    fruitMutableSet.add("小米")
    for (item in fruitMutableSet) {
        println(item)
    }

    val phoneBrandMap = mutableMapOf<String, String>("apple" to "苹果")
    phoneBrandMap["xiaomi"] = "小米"
    println(phoneBrandMap["apple"])
    println(phoneBrandMap["xiaomi"])

//    lambda
    val lambdaList = mutableListOf<String>("大东华", "小呗", "北京有套房")
    println(lambdaList.maxBy {it -> it.length})

    fun getLen() {
//        if (str != null) {
//            str.uppercase() // 这里会报错，因为不确定其他线程会不会修改stu为null
//        }
        str?.let {
            println(it.uppercase())
        }
    }
    getLen()

}